package com.finalministry.tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.smile.SmileFactory;

/**
 * <pre>
 * The file format is gzipped smile format. After that it's more or less binary json.
 * 
 * So first gunzip it, then use the java class from jackson smile format to read the file.
 * Attached how files are saved/loaded.
 * 
 * (pixelslost@gmail.com) from brother Marcus
 * </pre>
 * 
 * @author lussopi
 */
public class ImportExportMinistryAssistantBackup {

   public static void main(String[] args) throws JsonProcessingException, IOException {

      String gzippedFile = args[0];
      String jsonFile = gzippedFile + ".json";
      // First unzip the file
      gunzipIt(gzippedFile, jsonFile);

      ObjectMapper mapper = new ObjectMapper(new SmileFactory());
      JsonNode node = mapper.readTree(new File(jsonFile));


      
      

   }

   public static void gunzipIt(String gzippedFile, String targetGunzippedFile) {

      byte[] buffer = new byte[1024];

      try {

         GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(gzippedFile));

         FileOutputStream out = new FileOutputStream(targetGunzippedFile);

         int len;
         while ((len = gzis.read(buffer)) > 0) {
            out.write(buffer, 0, len);
         }

         gzis.close();
         out.close();

         System.out.println("Done");

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}
